define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'collect/index' + location.search,
                    add_url: 'collect/add?type=1',
                    edit_url: 'collect/edit',
                    del_url: 'collect/del',
                    multi_url: 'collect/multi',
                    import_url: 'collect/import',
                    table: 'collect',
                }
            });

            var table = $("#table");
            $('.btn-add').data('area', ['50%', '80%']);
            $('.btn-edit').data('area', ['50%', '80%']);
            table.on('post-body.bs.table', function(){
                $('.btn-editone').data('area', ['50%', '80%']);
            })

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='groupIds']", form).addClass("selectpage").data("source", "auth/group/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'date desc,device.mobile_serial asc,id desc',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'groupIds', title: __('分组'), visible: false},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'device.mobile_serial', 
                            title: __('Device.mobile_serial'),
                            sortable: true,
                            formatter: function(value, row, index) {
                                let color_class = '';
                                if(row.account_status == 0){
                                    color_class = 'text-purple';
                                }else if(row.account_status == 2){
                                    color_class = 'text-red';
                                }

                                if(row.account_status != 1){
                                    return '<b class="'+ color_class + '">' + value + '</b>'
                                }

                                return '<a href="javascript:;" data-url="collect/add?type=1&account_id=' + row.account_id + '" title="添加明细" class="dialogit ' + color_class + '" data-area=\'["50%", "80%"]\'><b>' + value + '</b></a>'
                            }
                        },
                        {field: 'account.id', title: __('账号id')},
                        {field: 'account.is_open', title: __('是否开好橱窗'), searchList: {"0":__('否'),"1":__('是')}, formatter: Table.api.formatter.normal},
                        {field: 'account.email', title: __('Account.email'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.search},
                        {
                            field: 'link_url', 
                            title: __('手机现有账号名字'), 
                            operate: false, 
                            table: table, 
                            class: 'autocontent', 
                            formatter: Table.api.formatter.url,
                        },
                        {field: 'video_name', title: __('Video_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'deduct_points', title: __('Deduct_points'), operate: false},
                        {field: 'volume', title: __('Volume'), sortable: true, operate: false},
                        {field: 'play_num_str', title: __('Play_num'), sortable: true, operate: false},
                        {
                            field: 'fans', 
                            title: __('粉丝量'), 
                            sortable: true,
                            operate: false
                        },
                        {
                            field: 'fans_diff', 
                            title: __('粉丝差值 '), 
                            operate: false,
                            formatter: function (value, row, index) {
                                if(row.fans > row.yestoday_fans){
                                    return '<span style="color:red;"><i class="fa fa-long-arrow-up"></i> '+row.fans_diff+'</span>';
                                }else if(row.fans < row.yestoday_fans){
                                    return '<span style="color:green;"><i class="fa fa-long-arrow-down"></i> '+row.fans_diff+'</span>';
                                }
                                return value;
                            }
                        },
                        {field: 'yestoday_fans', title: __('昨日粉丝量'), operate: false},
                        {field: 'device.ip', title: __('手机IP'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'date', title: __('日期'), operate:'RANGE', addclass:'datetimerange', autocomplete: false, sortable: true},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'admin.nickname', title: __('负责人'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    filter.type = 1;
                    op.type = "=";
                    
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
            });

            $('#date').val(Config.date);
            $(".form-commonsearch").find('button[type=submit]').trigger('click');
            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');
        },

        home: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'collect/home' + location.search,
                    add_url: 'collect/add?type=2',
                    edit_url: 'collect/edit',
                    del_url: 'collect/del',
                    multi_url: 'collect/multi',
                    import_url: 'collect/import',
                    table: 'collect',
                }
            });

            var table = $("#table");

            $('.btn-add').data('area', ['50%', '80%']);
            $('.btn-edit').data('area', ['50%', '80%']);
            table.on('post-body.bs.table', function(){
                $('.btn-editone').data('area', ['50%', '80%']);
            })

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='groupIds']", form).addClass("selectpage").data("source", "auth/group/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'date desc,device.mobile_serial asc,id desc',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'groupIds', title: __('分组'), visible: false},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'device.mobile_serial', 
                            title: __('Device.mobile_serial'),
                            sortable: true,
                            formatter: function(value, row, index) {
                                let color_class = '';
                                if(row.account_status == 0){
                                    color_class = 'text-purple';
                                }else if(row.account_status == 2){
                                    color_class = 'text-red';
                                }

                                if(row.account_status != 1){
                                    return '<b class="'+ color_class + '">' + value + '</b>'
                                }

                                return '<a href="javascript:;" data-url="collect/add?type=2&account_id=' + row.account_id + '" title="添加明细" class="dialogit ' + color_class + '" data-area=\'["50%", "80%"]\'><b>' + value + '</b></a>'
                            }
                        },
                        {field: 'account.id', title: __('账号id')},
                        {field: 'account.is_open', title: __('是否开好橱窗'), searchList: {"0":__('否'),"1":__('是')}, formatter: Table.api.formatter.normal},
                        {field: 'account.email', title: __('Account.email'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.search},
                        {
                            field: 'link_url', 
                            title: __('手机现有账号名字'), 
                            operate: false, 
                            table: table, 
                            class: 'autocontent', 
                            formatter: Table.api.formatter.url,
                        },
                        {field: 'video_name', title: __('Video_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'volume', title: __('Volume'), sortable: true, operate: false},
                        {field: 'play_num_str', title: __('Play_num'), operate: false},
                        {field: 'turnover', title: __('Turnover'), sortable: true, operate: false},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN', sortable: true},
                        {field: 'device.ip', title: __('手机IP'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'date', title: __('日期'), operate:'RANGE', addclass:'datetimerange', autocomplete: false, sortable: true},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete: false},
                        {field: 'admin.nickname', title: __('负责人'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    filter.type = 2;
                    op.type = "=";
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
            });

            $('#date').val(Config.date);
            $(".form-commonsearch").find('button[type=submit]').trigger('click');

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');
        },

        violent: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'collect/violent' + location.search,
                    add_url: 'collect/add?type=3',
                    edit_url: 'collect/edit',
                    del_url: 'collect/del',
                    multi_url: 'collect/multi',
                    import_url: 'collect/import',
                    table: 'collect',
                }
            });

            var table = $("#table");

            $('.btn-add').data('area', ['50%', '80%']);
            $('.btn-edit').data('area', ['50%', '80%']);
            table.on('post-body.bs.table', function(){
                $('.btn-editone').data('area', ['50%', '80%']);
            })

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='groupIds']", form).addClass("selectpage").data("source", "auth/group/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'date desc,device.mobile_serial asc,id desc',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'groupIds', title: __('分组'), visible: false},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'device.mobile_serial', 
                            title: __('Device.mobile_serial'),
                            sortable: true,
                            formatter: function(value, row, index) {
                                let color_class = '';
                                if(row.account_status == 0){
                                    color_class = 'text-purple';
                                }else if(row.account_status == 2){
                                    color_class = 'text-red';
                                }

                                if(row.account_status != 1){
                                    return '<b class="'+ color_class + '">' + value + '</b>'
                                }

                                return '<a href="javascript:;" data-url="collect/add?type=2&account_id=' + row.account_id + '" title="添加明细" class="dialogit ' + color_class + '" data-area=\'["50%", "80%"]\'><b>' + value + '</b></a>'
                            }
                        },
                        {field: 'account.id', title: __('账号id')},
                        {field: 'account.is_open', title: __('是否开好橱窗'), searchList: {"0":__('否'),"1":__('是')}, formatter: Table.api.formatter.normal},
                        {field: 'account.email', title: __('Account.email'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.search},
                        {
                            field: 'link_url', 
                            title: __('手机现有账号名字'), 
                            operate: false, 
                            table: table, 
                            class: 'autocontent', 
                            formatter: Table.api.formatter.url,
                        },
                        {field: 'video_name', title: __('Video_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'volume', title: __('Volume'), sortable: true, operate: false},
                        {field: 'play_num_str', title: __('Play_num'), operate: false},
                        {field: 'violent_num', title: __('违规数量'), operate:'BETWEEN', sortable: true},
                        {field: 'amount', title: __('视频佣金'), operate:'BETWEEN', sortable: true},
                        {field: 'device.ip', title: __('手机IP'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'date', title: __('日期'), operate:'RANGE', addclass:'datetimerange', autocomplete: false, sortable: true},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete: false},
                        {field: 'admin.nickname', title: __('负责人'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    filter.type = 3;
                    op.type = "=";
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
            });

            $('#date').val(Config.date);
            $(".form-commonsearch").find('button[type=submit]').trigger('click');

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');
        },

        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'collect/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'collect/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'collect/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            
        }
    };
    return Controller;
});
